package com.dyz.leetcode._04_bitOperation;

public class _191_hammingWeight {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int res = 0;
        for(int i=1; i<=32; i++){
            if((n&(1<<(i-1)))!=0){
                res++;
            }
        }
        return res;
    }

    public int hammingWeight1(int n) {
        int res = 0;
        for(int i=1; i<=32; i++){
            if((n&(1<<(i-1)))!=0){
                //有符号右移和无符号右移都是一样的
                //因为我们只右移动32位
                res+=n&1;
                n>>=1;
            }
        }
        return res;
    }

    public int hammingWeight2(int n) {
        int res=0;
        while(n!=0){
            n = n&(n-1);
            res++;
        }
        return res;
    }
}
